var util = require('../../libs/util.js');
var app = getApp();
Page({
  data: {
    driverData: [],
    searchShowed: false,
    searchVal: "",
    selected: false
  },
  onShareAppMessage: function () {
    return {
      title: app.appName,
      path: '/pages/index/index'
    }
  },
  onLoad: function (e) {
    wx.setNavigationBarTitle({ "title": app.appName +"--选择司机" });
    wx.showToast({
      title: "页面加载中\n请稍候...",
      icon: "loading",
      duration: 5000
    });
    this.setData({
      id:e.id,
      CO_ID:app.globalData.CO_ID,
      type:e.type,
      sub:e.sub == 'true'?true:false,
      url:e.url||''
    })
    this.loadDriverData();
  },
  onReady: function () {
    wx.hideToast()
  },
  showSearch: function () {
    this.setData({
      searchShowed: true
    });
  },
  hideSearch: function () {
    this.setData({
      searchVal: "",
      searchShowed: false
    });
    this.loadDriverData();
  },
  clearSearch: function () {
    this.setData({
      searchVal: ""
    });
    this.loadDriverData();
  },
  searchTyping: function (e) {
    this.setData({
      searchVal: e.detail.value
    });
    this.loadDriverData();
  },
  loadDriverData() {
    wx.showLoading({
      title: '加载数据中...',
    });
    var that = this;
    util.getData("driverSelect", { 
      CO_ID: app.globalData.CO_ID,
      searchVal: that.data.searchVal
    }, function (res, error) {
      console.log(res); console.log(error)
      wx.hideLoading();
      if (res.data.success) {
        var _data = res.data.data, _data_ = [];
        for (var i = 0; i < _data.length; i++) {
          _data_.push({ name: _data[i].name, phone: _data[i].phone, value: _data[i].ID});
        };
        that.setData({
          driverData: _data_
        })
      } else {
        that.setData({
          driverData: []
        })
      }
    });
  },
  setDriverData: function (text) {
    var that = this;
    var data = that.data.dataList, _data_ = [];
    for (var i = 0; i < data.length; i++) {
      if (String(data[i].name).indexOf(text) > -1 || String(data[i].phone).indexOf(text) > -1 || String(data[i].ID).indexOf(text) > -1) {
        _data_.push({ name: data[i].name, phone: data[i].phone, value: data[i].value});
      }
    };
    that.setData({
      driverData: _data_
    })
  },
  driverSelect: function (e) {
    console.log(e)
    var driverData = this.data.driverData,selectData = {};
    for (var i = 0; i < driverData.length; i++) {
      driverData[i].checked = driverData[i].value == e.detail.value;
      if (driverData[i].value == e.detail.value) {
        selectData = driverData[i];
        this.setData({
          selected: true
        })
      }
    }
    this.setData({
      driverData: driverData,
      selectData: selectData
    });
  },
  confirmData: function () {
    var that = this;
    if (this.data.selected) {
      if (this.data.type == "report") {
        wx.setStorage({
          key: "driver",
          data: that.data.selectData,
          complete: function (res) {
            wx.navigateBack({
              delta: 1
            });
          }
        });
      } else {
        var driverData = that.data.driverData;
        for (var i = 0; i < driverData.length; i++) {
          if (driverData[i].checked) {
            that.setData({
               currentDriver:driverData[i]
            })
            that.submitBind()
            return false;
          }
        }
      }
    } else {
      wx.showToast({
        title: "请选择司机",
        image: "/images/wrong.svg",
        duration: 3000
      });
    }
  },
  submitBind:function(force){
    var that = this;
    util.putData("vehicleBindDriver", {
      vehicleId: that.data.id,
      driverId: that.data.currentDriver.value,
      openid: app.globalData.openid,
      CO_ID: app.globalData.CO_ID,
      sub:that.data.sub?'true':'false',
      force:force?'true':'false'
    }, function (res, error) {
      console.log(res); console.log(error)
      wx.hideLoading();
      if (error) {
        wx.showToast({
          title: res.data.data,
          image: '/images/wrong.png',
          duration: 3000
        });
        return false;
      }
      if (res.data.success) {
        wx.showToast({
          title: res.data.data,
          icon: 'success',
          success: function () {
            setTimeout(function () {
              if(that.data.url){
                wx.redirectTo({
                  url: that.data.url
                })
              }else{
                wx.navigateBack({
                  delta: 1
                });
              }
              
            }, 3000);
          }
        });
      } else {
        if (res.data.hasResult) {
          wx.showModal({
            title: '绑定失败',
            content: res.data.data,
            showCancel: true,
            cancelText:'关闭',
            confirmText:'强制绑定',
            success: function (res) {
              that.submitBind(true)
            }
          })
          return false;
        } else {
          wx.showToast({
            title: res.data.data,
            image: '/images/wrong.png',
            duration: 3000
          });
          return false;
        }
      }
    });
  },
  cancelConfirmData: function () {
    var that = this;
    if (this.data.type == "report") {
      wx.setStorage({
        key: "driver",
        data: {},
        complete: function (res) {
          if(that.data.url){
            wx.redirectTo({
              url: that.data.url
            })
          }else{
            wx.navigateBack({
              delta: 1
            });
          }
        }
      });
    } else {
      if(that.data.url){
        wx.redirectTo({
          url: that.data.url
        })
      }else{
        wx.navigateBack({
          delta: 1
        });
      }
    }
  }
});